<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane label="企业检查列表" name="first"></el-tab-pane>
      <!-- <el-tab-pane label="我参与的检查" name="second"></el-tab-pane> -->
      <!-- <el-tab-pane label="我的待办" name="third"></el-tab-pane> -->
    </el-tabs>
    <el-form
      :model="queryParams"
      ref="queryForm"
      size="small"
      :inline="true"
      v-show="showSearch"
      label-width="100px"
    >
      <el-form-item
        label="检查类型"
        prop="checkName"
        v-if="activeName == 'first'"
      >
        <treeselect
          style="width: 260px"
          v-model="queryParams.checkType"
          :options="deptOptions"
          :default-expand-level="100"
          placeholder="请选择作检查类型"
        />
      </el-form-item>
      <el-form-item label="日期范围" v-if="activeName == 'first'">
        <el-date-picker
          v-model="queryParams.date"
          type="daterange"
          range-separator="至"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
          @keyup.enter.native="handleQuery"
          value-format="yyyy-MM-dd"
        >
        </el-date-picker>
      </el-form-item>

      <el-form-item prop="checkName" v-if="activeName == 'second'">
        <el-input
          v-model="queryParams.checkName"
          placeholder="请输入检查任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item prop="checkName" v-if="activeName == 'third'">
        <el-input
          v-model="queryParams.checkName"
          placeholder="请输入检查任务名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item v-if="activeName == 'first'">
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8" v-if="activeName == 'first'">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['enter:enter:add']"
          >新增检查任务</el-button
        >
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['enter:enter:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['enter:enter:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['enter:enter:export']"
        >导出</el-button>
      </el-col> -->
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="enterList" :key="key">
      <el-table-column label="序号" type="index" align="center" width="50" />
      <el-table-column
        label="检查项目"
        v-if="activeName == 'third'"
        align="center"
        prop="project"
        :key="key+4"
      />
      <el-table-column label="检查任务名称" align="center" prop="checkName" :key="key+5" />
      <el-table-column
        label="检查类型"
        v-if="activeName == 'third'"
        align="center"
        prop="checkType"
        :key="key+3"
      />
      <el-table-column label="组织名称" align="center" prop="deptName"  :key="key+1"/>
      <el-table-column label="创建时间" align="center" prop="createTime">
        <template slot-scope="scope">
          {{ parseTime(scope.row.createTime, "{y}-{m}-{d}") }}
        </template>
      </el-table-column>
      <el-table-column label="创建人" align="center" prop="createBy" :key="key+2"/>
      <el-table-column
        label="状态"
        v-if="activeName != 'third'"
        align="center"
        prop="checkStatus"
      >
        <template slot-scope="scope">
          <el-tag
            effect="dark"
            :color="
              scope.row.checkStatus == '1'
                ? rgb1
                : scope.row.checkStatus == '2'
                ? rgb2
                : scope.row.checkStatus == '3'
                ? rgb3
                : rgb4
            "
            style="border: 0"
          >
            <dict-tag
              :options="dict.type.check_status"
              :value="scope.row.checkStatus"
            />
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="检查进度"
        align="center"
        v-if="activeName != 'third'"
        prop="checkLoading"
      />
      <el-table-column
        label="项目质量整改率"
        v-if="activeName != 'third'"
        align="center"
        prop="hiddenDate"
      >
        <template slot-scope="scope"> <span v-if="scope.row.hiddenDate">{{ scope.row.hiddenDate }}% </span></template>
      </el-table-column>
      <!-- <el-table-column
        label="检查情况"
        v-if="activeName != 'third'"
        align="center"
        prop="checkSituation"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            @click="dialysisData(scope.row)"
          >
            数据透视
          </el-button>
        </template>
      </el-table-column> -->
      <el-table-column
        v-if="activeName == 'third'"
        label="检查依据"
        align="center"
        prop="checkBasis"
      >
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.check_according"
            :value="scope.row.checkBasis"
          />
        </template>
      </el-table-column>
      <!-- <el-table-column label="检查说明" align="center" prop="checkExplain" />
      <el-table-column label="任务分配类型" align="center" prop="checkClassify">
        <template slot-scope="scope">
          <dict-tag
            :options="dict.type.check_classify"
            :value="scope.row.checkClassify"
          />
        </template>
      </el-table-column> -->
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['enter:enter:edit']"
            >查看</el-button
          >
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['enter:enter:remove']"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="handleClick('1')"
    />
    <!-- 数据透视 -->
    <el-dialog :title="title" :visible.sync="dialogVisible" width="60%">
      <div v-if="dialogVisible">
        <div>
        本次检查覆盖{{ PiovtData.checkProjectNum}}个工程项目，共检查出安全隐患{{ PiovtData.hiddenNum }}条，其中一般隐患{{
          PiovtData.normalHiddenNum
        }}条，重大隐患{{ PiovtData.majorHiddenNum }}条
      </div>

      <div
        id="newEcharts"
        style="width: 100%; height: 400px; padding-top: 40px"
      ></div>
      <div
        id="newEchartsBtm"
        style="width: 100%; height: 400px; padding-top: 40px"
      ></div>
      
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="dialogVisible = false"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  listEnter,
  getEnter,
  delEnter,
  updateEnter,
  getEnterDel,
  EnterPiovt,
} from "@/api/biz/enter/enter";
import { mapState } from "vuex";
import { getTree } from "@/api/biz/enter/enterNext";
import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";
export default {
  name: "Enter",
  dicts: ["check_status", "check_according"],
  components: {
    Treeselect,
  },
  data() {
    return {
      PiovtData: {
        checkName:null,
        checkProjectNum:null,
        hiddenNum:null,
        majorHiddenNum:null,
        normalHiddenNum:null,
        hiddenTypeStatistics:[],//圆
        projectStatistics:[],//柱
      },
      rgb1: "rgb(147, 210, 243)",
      rgb2: "rgb(255, 191, 107)",
      rgb3: "rgb(162, 239, 77)",
      rgb4: "rgb(144, 147, 153)",
      activeName: "first",
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 公司检查表格数据
      enterList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      dialogVisible: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        projectId: this.$store.state.app.selectData,
        checkName: null,
        checkType: null,
        deptName: null,
        checkStatus: null,
        checkLoading: null,
        hiddenDate: null,
        checkSituation: null,
        checkBasis: null,
        checkExplain: null,
        checkClassify: null,
        date: null,
      },
      // 表单参数
      form: {},
      // 部门树选项
      deptOptions: [],
      typeNoData: {
        checkTypeNo: null,
      },
      key:0,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询公司检查列表 */
    getList() {
      this.loading = true;
      listEnter(this.queryParams).then((response) => {
        this.enterList = response.rows;
        this.total = response.total;
        this.key=this.key++
        this.loading = false;
      });
      getTree(this.typeNoData).then((response) => {
        this.deptOptions = response.data;
      });
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        checkName: null,
        checkType: null,
        deptName: null,
        checkStatus: null,
        checkLoading: null,
        hiddenDate: null,
        checkSituation: null,
        checkBasis: null,
        checkExplain: null,
        checkClassify: null,
        delFlag: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        projectId: this.$store.state.app.selectData,
        checkName: null,
        checkType: null,
        deptName: null,
        checkStatus: null,
        checkLoading: null,
        hiddenDate: null,
        checkSituation: null,
        checkBasis: null,
        checkExplain: null,
        checkClassify: null,
        date: null,
      };
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.title = "添加公司检查";
      this.$router.push({ name: "enterNext" });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
       this.$confirm('此操作将永久删除该数据, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delEnter(row.id).then(() => {
        this.$message({
          message: '删除成功',
          type: 'success'
        });
      });
      this.handleClick()
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });          
        });
      
    },
    // 查看
    handleUpdate(row) {
      if (this.activeName == 'first') {
        this.$router.push({ name: "enterLook",query:{id: row.id,checkClassify:row.checkClassify} });
      }else if(this.activeName == 'second'){
        this.$router.push({ name: "enterMyLook",query:{id: row.id,checkClassify:row.checkClassify} });
      }else{
        this.$router.push({ name: "enterMyLookDate", query: { id: row.enterCheckProjectId ,ids:row.enterCheckId} });
      }
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "enter/enter/export",
        {
          ...this.queryParams,
        },
        `enter_${new Date().getTime()}.xlsx`
      );
    },
    // 切换导航栏
    handleClick(data) {
      if (data!=1) {
        this.resetQuery()
      }
      if (this.activeName == "first") {
          listEnter(this.queryParams).then((response) => {
          this.enterList = response.rows;
          this.total = response.total;
          this.loading = false;
          this.key=this.key++
        });
      } else if (this.activeName == "third") {
        getEnterDel(this.queryParams).then((response) => {
          this.enterList = response.rows;
          this.total = response.total;
          this.key=this.key++
        });
      } else {
        getEnter(this.queryParams).then((response) => {
          this.enterList = response.rows;
          this.total = response.total;
          this.key=this.key++
        });
      }
    },
    // 数据透视
    dialysisData(data) {
      this.PiovtData = {
        checkName:null,
        checkProjectNum:null,
        hiddenNum:null,
        majorHiddenNum:null,
        normalHiddenNum:null,
        hiddenTypeStatistics:[],//圆
        projectStatistics:[],//柱
      },
      EnterPiovt(data.id).then((response) => {
        this.PiovtData = response.data;
        this.title = this.PiovtData.checkName + "——数据透视";
        this.dialogVisible = true;
        this.$nextTick(() => {
          //  执行echarts方法
          this.initEcharts();
          this.initEchartsBtm()
        });
      });
    },

    initEcharts() {
      let arrx = [];
      let arry = [];
      this.PiovtData.projectStatistics.forEach((item) => {
        arrx.push(item.projectName);
        arry.push(item.hiddenNum);
      });
      var echarts = require("echarts");

      // 基于准备好的dom，初始化echarts实例
      const myChart = echarts.init(document.getElementById("newEcharts"));
      // 绘制图表
      const option = {
        title: {
          text: "覆盖项目隐患数量统计",
        },
        tooltip: {},
        xAxis: {
          data: arrx,
        },
        yAxis: {},
        series: [
          {
            name: arrx,
            type: "bar",
            data: arry,
          },
        ],
      };
      myChart.setOption(option);
    },
    initEchartsBtm() {
      let arrx = [];
      let arry = [];
      this.PiovtData.hiddenTypeStatistics.forEach((item) => {
        let data ={
          value: null, 
          name: null,
        }
        data.value = item.num
        data.name = item.hiddenType
        arrx.push(data);
      });
      var echarts = require("echarts");

      // 基于准备好的dom，初始化echarts实例
      const myChart = echarts.init(document.getElementById("newEchartsBtm"));

      const option = {
        title: {
          text: "隐患类别统计",
          left: "left",
        },
        tooltip: {
          trigger: "item",
        },
        legend: {
          orient: "vertical",
          left: "center",
          orient: 'horizontal',
        },
        series: [
          {
            name: "隐患类别统计",
            type: "pie",
            radius: "50%",
            data: arrx,
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: "rgba(0, 0, 0, 0.5)",
              },
            },
          },
        ],
      };

      option && myChart.setOption(option);
    },
  },
  watch: {
    app: {
      handler(newVal, oldVal) {
        this.queryParams.projectId = newVal.selectData;
        this.getList();
      },
      deep: true,
    },
  },
  computed: {
    ...mapState(["app"]),
  },
};
</script>
